<template>
  <div>
    <div class="form-content" v-if="statusCheck" :key="1">
      <p class="title">3.0负责审核</p>
      <el-form :model="formDrawCheckData" :rules="ruleDrawCheckData" ref="form" class="form-input" label-width="140px">
        <p class="attention"><span>审核说明：</span>图纸审核</p>
        <el-row>
          <el-col :span="12">
            <el-form-item label="CAD文件审核">
              <el-radio v-model="formDrawCheckData.radio" label="1">通过</el-radio>
              <el-radio v-model="formDrawCheckData.radio" label="0">不通过</el-radio>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="审核意见" prop="CADsuggest">
              <el-input type="textarea" v-model="formDrawCheckData.CADsuggest"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="PDF文件审核">
              <el-radio v-model="formDrawCheckData.radio2" label="1">通过</el-radio>
              <el-radio v-model="formDrawCheckData.radio2" label="0">不通过</el-radio>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="审核意见" prop="PDFsuggest">
              <el-input type="textarea" v-model="formDrawCheckData.PDFsuggest"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row> </el-row>
        <el-row>
          <el-form-item label="备注" prop="remarks">
            <el-input type="textarea" v-model="formDrawCheckData.remarks"></el-input>
          </el-form-item>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="下一流程">
              <el-input v-model="nextNodeNameContent" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="formDrawCheckData.radio == 1 && formDrawCheckData.radio2 == 1">
            <el-form-item label="下一流程人员" prop="nextAuthor">
              <el-select placeholder="请选择下一流程人员" v-model="formDrawCheckData.nextAuthor" multiple @change="handleChange">
                <el-option v-for="item in nextAuthorLists" :key="item.id" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-else>
            <el-form-item label="上一流程人员">
              <el-input v-model="formDrawCheckData.nextAuthor2" readonly></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <div class="btn-option">
          <el-button @click="submit" type="primary" v-if="formDrawCheckData.radio == 1 && formDrawCheckData.radio2 == 1">通过</el-button>
          <el-button @click="rejecte" type="danger" v-else>驳回</el-button>
          <el-button @click="cancel">取消</el-button>
        </div>
      </el-form>
    </div>

    <div class="form-content" v-else :key="2">
      <p class="title">3.0负责审核</p>
      <el-form :model="formDrawCheckData2" class="form-input" label-width="140px">
        <p class="attention"><span>审核说明：</span>图纸审核</p>
        <el-row>
          <el-col :span="12">
            <el-form-item label="CAD文件审核">
              <el-radio v-model="formDrawCheckData2.radio" label="1" disabled>通过</el-radio>
              <el-radio v-model="formDrawCheckData2.radio" label="0" disabled>不通过</el-radio>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="审核意见">
              <el-input type="textarea" v-model="formDrawCheckData2.CADsuggest" readonly></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="PDF文件审核">
              <el-radio v-model="formDrawCheckData2.radio2" label="1" disabled>通过</el-radio>
              <el-radio v-model="formDrawCheckData2.radio2" label="0" disabled>不通过</el-radio>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="审核意见">
              <el-input type="textarea" v-model="formDrawCheckData2.PDFsuggest" readonly></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row> </el-row>
        <el-row>
          <el-form-item label="备注">
            <el-input type="textarea" v-model="formDrawCheckData2.remarks" readonly></el-input>
          </el-form-item>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="下一流程">
              <el-input v-model="nextNodeNameContent" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="上一流程人员" v-if="formDrawCheckData2.radio == 0 || formDrawCheckData2.radio2 == 0">
              <el-input v-model="formDrawCheckData2.nextAuthor2" readonly></el-input>
            </el-form-item>
            <el-form-item label="下一流程人员" v-else>
              <el-input v-model="formDrawCheckData2.nextAuthor" readonly></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
  </div>
</template>
<script>
import { EmbodimentCreate, getAuditFlowDetails, downloadFile } from '@/api/api'
import { nextNodeArr, getMultipleLabel, getName, getAccount, getAccountBack } from '@/unils/utils'
import Bus from '@/plugins/bus'
import swal from 'sweetalert'
import _ from 'underscore'
export default {
  props: {
    statusCheck: {
      type: Boolean,
      defalut: true
    }
  },
  data () {
    return {
      orunid: '',
      formDrawCheckData: {
        radio: '1', //通过1
        radio2: '1', // 通过2
        CADsuggest: '', //CAD建议
        PDFsuggest: '', // PED建议
        remarks: '', // 留言
        nextAuthor: [], // 申请人
        nextAuthor2: '',
        file1: [],
        file2: []
      },
      labelName: '',
      nextAuthorLists: [],
      nextNodeNameContent: '负责预算',
      ruleDrawCheckData: {
        CADsuggest: [{ required: true, message: '请填写审核意见!', trigger: 'blur' }],
        PDFsuggest: [{ required: true, message: '请填写审核意见!', trigger: 'blur' }],
        remarks: [{ required: true, message: '请填写备注!', trigger: 'blur' }],
        nextAuthor: [{ required: true, message: '请选择审核人!', trigger: 'change' }],
      },
      formDrawCheckData2: {
        radio: '1',
        radio2: '1',
        CADsuggest: '',
        PDFsuggest: '',
        remarks: '',
        nextAuthor: '',
        nextAuthor2: '',
        addName: '',
        file1: [],
        file2: []
      }
    }
  },
  mounted () {
    this.fun()
  },
  beforeDestroy () {
    Bus.$off('res')
    Bus.$off('resAuthor')
  },
  methods: {
    handleChange (val) {
      // this.labelName = getMultipleLabel(val, this.nextAuthorLists)
    },
    downFile1_1 (id) {
      downloadFile({ id: id }).then(res => {
        if (res.code == 200) {
          window.open(res.data.url, '_self')
        }
      }).catch(e => {
        console.log(e)
      })
    },
    downFile1_2 (id) {
      downloadFile({ id: id }).then(res => {
        if (res.code == 200) {
          window.open(res.data.url, '_self')
        }
      }).catch(e => {
        console.log(e)
      })
    },
    downFile2_1 (id) {
      downloadFile({ id: id }).then(res => {
        if (res.code == 200) {
          window.open(res.data.url, '_self')
        }
      }).catch(e => {
        console.log(e)
      })
    },
    downFile2_2 (id) {
      downloadFile({ id: id }).then(res => {
        if (res.code == 200) {
          window.open(res.data.url, '_self')
        }
      }).catch(e => {
        console.log(e)
      })
    },
    submit: _.debounce(function () {
      this.$refs.form.validate((valid) => {
        if (!valid) return false
        let msg = '是否确认通过审核!'
        this.confirm(msg, () => {
          let params = {
            radio8: this.formDrawCheckData.radio,
            radio9: this.formDrawCheckData.radio2,
            opinion1: this.formDrawCheckData.CADsuggest,
            opinion2: this.formDrawCheckData.PDFsuggest,
            remarks6: this.formDrawCheckData.remarks,
            orunid: this.orunid,
            status: 'current',
            type: 2,
            addName: this.formDrawCheckData2.addName,
            currentNodeName: '3.0负责图纸审核',
            currentNodeID: 'T10004',
            nextNodeID: 'T10005',
            nodeTowardsInfo: nextNodeArr(this.$store.state.jobAccount, this.formDrawCheckData.nextAuthor, 'T10005', '4.0负责预算', 'T10004', '3.0负责图纸审核'),
          }
          EmbodimentCreate(params)
            .then((res) => {
              if (res.code == 200) {
                this.alertok('提交成功', () => {
                  this.$router.go(-1)
                })
              } else {
                swal('错误', res.message || '网络连接错误', 'warning')
              }
            })
            .catch((err) => {
              swal('错误', '网络连接错误', 'error')
            })
        })
      })
    }, 800),
    rejecte: _.debounce(function () {
      this.$refs.form.validate((valid) => {
        if (!valid) return false
        let msg = '是否确认驳回!!'
        this.confirm(msg, () => {
          let nodeTowardsInfo = [
            { currentAuthor: this.$store.state.jobAccount, nextNodeID: 'T10003', nextAuthor: `${this.formDrawCheckData.nextAuthor2}`, nextNodeName: '2.0图纸修改驳回', currentNodeID: 'T10004', currentNodeName: '3.0负责图纸审核' }
          ]
          let params = {
            radio8: this.formDrawCheckData.radio,
            radio9: this.formDrawCheckData.radio2,
            opinion1: this.formDrawCheckData.CADsuggest,
            opinion2: this.formDrawCheckData.PDFsuggest,
            remarks6: this.formDrawCheckData.remarks,
            orunid: this.orunid,
            status: 'current',
            type: 0,
            addName: this.formDrawCheckData2.addName,
            currentNodeName: '3.0负责图纸审核',
            currentNodeID: 'T10004',
            nextNodeID: 'T10003',
            nodeTowardsInfo: JSON.stringify(nodeTowardsInfo)
          }
          // console.log(params)
          EmbodimentCreate(params)
            .then((res) => {
              // console.log(res)
              if (res.code == 200) {
                this.alertok('驳回成功', () => {
                  this.$router.go(-1)
                })
              } else {
                swal('错误', res.message || '网络连接错误', 'warning')
              }
            })
            .catch((err) => {
              swal('错误', '网络连接错误', 'error')
            })
        })
      })
    }, 800),
    cancel () {
      this.$router.go(-1)
    },
    fun () {
      // 接收下拉框人员数据
      Bus.$on('resAuthor', res => {
        this.nextAuthorLists = res
      })
      // 接收所有流程数据
      Bus.$on('res', res => {
        this.orunid = res.data.orunid
        this.formDrawCheckData2.radio = res.data.radio8
        this.formDrawCheckData2.radio2 = res.data.radio9
        this.formDrawCheckData2.CADsuggest = res.data.opinion1
        this.formDrawCheckData2.PDFsuggest = res.data.opinion2
        this.formDrawCheckData2.remarks = res.data.remarks6
        // 为false，也就是在不可编辑状态下（该流程已提交结束），读取该提交数据来显示
        if (!this.statusCheck) {
          // 当被驳回时，显示上一流程审核被谁驳回
          if (res.data.radio8 == 0 || res.data.radio9 == 0) {
            this.formDrawCheckData2.nextAuthor2 = getAccount(res.data.nodeTowardsInfo, 'T10004')
          } else {
            // 当通过时，显示下一流程人员
            this.formDrawCheckData2.nextAuthor = getAccount(res.data.nodeTowardsInfo, 'T10004')
          }
        } else {
          this.formDrawCheckData.nextAuthor2 = getAccountBack(res.data.nodeTowardsInfo, 'T10003')
        }
        this.formDrawCheckData2.addName = res.data.addName
        if (res.data.cadfile) {
          this.formDrawCheckData2.file1 = JSON.parse(res.data.cadfile)
          this.formDrawCheckData.file1 = JSON.parse(res.data.cadfile)
        }
        if (res.data.pdffile) {
          this.formDrawCheckData2.file2 = JSON.parse(res.data.pdffile)
          this.formDrawCheckData.file2 = JSON.parse(res.data.pdffile)
        }
        // 获取驳回到上一流程人的域账号
        this.formDrawCheckData.nextAuthor2 = getAccountBack(res.data.nodeTowardsInfo, 'T10003')
      })
    }
  }
}
</script>
<style lang="less" scoped>
.form-content {
  .attention {
    // margin-left: 80px;
    span {
      color: red;
    }
  }
  .title {
    font-size: 30px;
    text-align: center;
    margin-bottom: 40px;
  }
  .form-input {
    width: 70%;
    // background-color: blue;
    margin: auto;
    .btn-option {
      display: flex;
      justify-content: flex-end;
    }
  }
}
</style>